package com.woniu.inlet.web;

import com.woniu.outlet.po.ClientApplications;
import com.woniu.outlet.po.PotentialCustomers;
import com.woniu.service.IClientApplicationService;
import com.woniu.util.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/*
    将潜在的客户进行分配：有两种的分配的方式--1：是由客户经理自己来申请，最后由销售经理来审批，审批通过之后发送一条消息给客户经理，在这个同时
    修改潜在用户的状态，2：是由销售经理来分配的
 */
@RestController
@RequestMapping("/cli")
@Api(tags = "客户的申请")
public class ClientApplicationController {

    @Autowired
    private IClientApplicationService clientApplicationService;

    //客户经理自己来申请，最后由销售经理来审批
    @ApiOperation(value = "applications",notes = "客户经理自己来申请，最后由销售经理来审批")
    @PostMapping("/applications")
    @PreAuthorize("hasRole('client')")
    public ResponseResult<String> applications(@RequestBody ClientApplications clientApplication){
        int applications = clientApplicationService.applications(clientApplication);
        return new ResponseResult<>("申请成功！！",200);
    }

    //首先经理可以查看所有的没有处理的申请书
    @ApiOperation(value = "findapplications",notes = "首先经理可以查看所有的没有处理的申请书")
    @GetMapping("/findapplications")
    @PreAuthorize("hasRole('market')")
    public ResponseResult<List<ClientApplications>> findapplications(){
        List<ClientApplications> clientApplicationsList = clientApplicationService.findapplications();
        //System.out.println(clientApplicationsList.toString());
        return new ResponseResult<>(clientApplicationsList,200);
    }


    //营销经理来处理发过来的申请(默认经理是同意的),这个时侯经理给客户经理发送手机消息，需要使用到队列
    @ApiOperation(value = "handleapplications",notes = "营销经理来处理发过来的申请(默认经理是同意的),这个时侯经理给客户经理发送手机消息")
    @PostMapping("/handleapplications")
    @PreAuthorize("hasRole('market')")
    public ResponseResult<String> handleapplications(@RequestBody ClientApplications clientApplications){
        //修改申请书的状态，修改潜在客户的信息（被谁关户，以及关户的状态）
        int handleapplications = clientApplicationService.handleapplications(clientApplications);
        return new ResponseResult<>("申请成功！！",200);
    }

    //客户经理发来的申请，营销经理没有同意，拒绝了
    @ApiImplicitParams({
            @ApiImplicitParam(value = "请求的id",name = "app_id",required = true,paramType = "query",dataType = "Integer")
    })
    @ApiOperation(value = "rejectapplication",notes = "客户经理发来的申请，营销经理没有同意，拒绝了")
    @GetMapping("/rejectapplication")
    @PreAuthorize("hasRole('market')")
    public ResponseResult<String> rejectapplication(Integer app_id){
        Integer rejectapplication = clientApplicationService.rejectapplication(app_id);
        return new ResponseResult<>("营销经理已经拒绝了你的申请！！",200);
    }


    //第二种管理潜在客户的方法：是由营销经理直接对潜在客户进行分配
    // ，这个也涉及到分布式事务的一致性！！
    @ApiOperation(value = "distributionpotentialcustomer",notes = "营销经理直接对潜在客户进行分配")
    @PostMapping("/distributionpotentialcustomer")//参数只需要pcid 以及pcworkerid
    @PreAuthorize("hasRole('market')")
    public ResponseResult<String> distributionpotentialcustomer(@RequestBody PotentialCustomers potentialCustomers){
        //分配潜在客户给客户经理
        Integer distributionpotentialcustomer = clientApplicationService.distributionpotentialcustomer(potentialCustomers);
        return new ResponseResult<>("分配成功，请客户经理注意处理！！",200);
    }


}
